<?php
namespace App\Controller\Common;

use App\Base\BaseController;
use App\BaseExt\PicUpload;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;



class MyUploadController extends BaseController
{

    /**
     * @Route("/MyWebPicUpload" , name="MyWebPicUpload")
     */
    public function MyWebPicUpload()
    {
        if (!$name = $this->getParamRequired("name")) {
            $name = "Filedata";
        }
        $picUploadModel = new PicUpload();
        $rst = $picUploadModel->doUpload($name);
        $data = $picUploadModel->getData();
        if ($data) {
            $re = array(
                "s"=>1,
                "i"=>"上传成功",
                "d"=>$data[$name],
            );

            //上传到OSS
//            require_once  '../assets/Oss/samples/Common.php';
//
//
//            $bucket = Common::getBucketName();
//            $ossClient = Common::getOssClient();
//




        } else {
            $re = $rst;
        }
        return new JsonResponse($re);
    }

    /**
     * @Route("/MyMultiPicUpload" , name="MyMultiPicUpload")
     */
    public function MyMultiPicUpload(Request $request)
    {
        if (!$name = $this->getParamRequired("name")) {
            $name = "Filedata";
        }
        $files = $request->files->get("Filedata");
        dump($files);
        $picUploadModel = new PicUpload();
        $rst = $picUploadModel->multiUpload($name);
        $data = $picUploadModel->getData();
        if ($data) {
            $re = array(
                "s"=>1,
                "i"=>"上传成功",
                "d"=>$data[$name],
            );
        } else {
            $re = $rst;
        }
        return new JsonResponse($re);
    }

    /**
     * @Route("/MyFileUpload" , name="MyFileUpload")
     */
    public function MyFileUpload()
    {
        $request = Request::createFromGlobals();
        //获取参数
        if (!$uploadDir  = $this->getParamRequired("dir",$request)) {$uploadDir = "default";}
        //检查上传格式是否正确

        //尝试上传
        /**@var $file UploadedFile*/
        $file = $request->files->get("Filedata");
        $orgName = $file->getClientOriginalName();
        $arr = mb_split(".",$orgName);

//        var_dump($file);
//        var_dump(pathinfo($orgName)["extension"]);
//        var_dump($arr[count($arr) -1]);

//        $fileName = md5(time().uniqid()).'.'.$file->guessExtension();
        $fileName = md5(time().uniqid()).'.'.pathinfo($orgName)["extension"];
        $absPath = './uploads/'.$uploadDir.'/'.date("Ymd");
        $file->move($absPath, $fileName);
        //检查像素宽高限制，超出限制的清除文件
        $filePath = "/uploads/".$uploadDir."/".date("Ymd")."/".$fileName;
        $realPath = $absPath."/".$fileName;

        $re = array(
            's'=>1,
            'i'=>$orgName,
            "d"=>$filePath
        );
        return new JsonResponse($re);
    }
}
